﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GalaxyDefenderChaos
{
    public class BillBoard : VisibleGameEntity
    {
        private Vector2 _Size;

        public Vector2 Size
        {
            get { return _Size; }
            set { _Size = value; }
        }

        private Texture2D _Texture;
        private ContentManager _Content;

        private VertexPositionNormalTexture[] _Vertices;

        public VertexPositionNormalTexture[] Vertices
        {
            get { return _Vertices; }
            set { _Vertices = value; }
        }

        private short[] _Indexes;
        private Vector3 _Position;

        public Vector3 Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        private Vector3 _Normal;
        private Vector3 _Up;
        private VertexDeclaration _VertexDeclaration;
        private BasicEffect quadEffect;
        private Camera _Camera;

        public BillBoard(ContentManager content, string texture, Vector2 size, Vector3 position, Vector3 up, GraphicsDevice graphicDevice, Camera camera)
        {
            _Content = content;
            _Size = size;
            _Position = position;
            _Up = up;
            _Texture = _Content.Load<Texture2D>(texture);
            quadEffect = new BasicEffect(graphicDevice);
            _Camera = camera;
            initVertices();
        }

        private void initVertices()
        {
            _Vertices = new VertexPositionNormalTexture[4];
            _Indexes = new short[6];
            _Normal = CalculateNormal();
            Vector3 Left = Vector3.Cross(_Normal, _Up);
            Vector3 uppercenter = (_Up * _Size.Y / 2) + _Position;
            Vector3 UpperLeft = uppercenter + (Left * _Size.X / 2);
            Vector3 UpperRight = uppercenter - (Left * _Size.X / 2);
            Vector3 LowerLeft = UpperLeft - (_Up * _Size.Y);
            Vector3 LowerRight = UpperRight - (_Up * _Size.Y);

            Vector2 textureUpperLeft = new Vector2(1.0f, 0.0f);
            Vector2 textureUpperRight = new Vector2(0.0f, 0.0f);
            Vector2 textureLowerLeft = new Vector2(1.0f, 1.0f);
            Vector2 textureLowerRight = new Vector2(0.0f, 1.0f);

            // UV texture coordinates
            //Vector2 textureTopLeft = new Vector2(1.0f, 0.0f);
            //Vector2 textureTopRight = new Vector2(0.0f, 0.0f);
            //Vector2 textureBottomLeft = new Vector2(1.0f, 1.0f);
            //Vector2 textureBottomRight = new Vector2(0.0f, 1.0f);

            // Provide a normal for each vertex
            for (int i = 0; i < _Vertices.Length; i++)
            {
                _Vertices[i].Normal = _Normal;
            }

            _Vertices[0].Position = LowerLeft;
            _Vertices[0].TextureCoordinate = textureLowerLeft;
            _Vertices[1].Position = UpperLeft;
            _Vertices[1].TextureCoordinate = textureUpperLeft;
            _Vertices[2].Position = LowerRight;
            _Vertices[2].TextureCoordinate = textureLowerRight;
            _Vertices[3].Position = UpperRight;
            _Vertices[3].TextureCoordinate = textureUpperRight;

            _Indexes[0] = 0;
            _Indexes[1] = 1;
            _Indexes[2] = 2;
            _Indexes[3] = 2;
            _Indexes[4] = 1;
            _Indexes[5] = 3;

        }

        private Vector3 CalculateNormal()
        {
            return Vector3.Normalize(_Camera.CameraTarget - _Camera.CameraPosition);
        }

        public override void Update(GameTime gameTime, TouchCollection touchCollection)
        {
            initVertices();
        }

        public override void Draw(GraphicsDevice graphicsDevice, Camera camera, GameTime gameTime)
        {
            graphicsDevice.RasterizerState = RasterizerState.CullNone;
            //graphicsDevice.BlendState = BlendState.AlphaBlend;
            quadEffect.EnableDefaultLighting();

            quadEffect.World = Matrix.CreateTranslation(_Position);
            quadEffect.View = camera.View;
            quadEffect.Projection = camera.Projection;
            quadEffect.TextureEnabled = true;
            quadEffect.Texture = _Texture;

            //quadEffect.FogEnabled = true;
            //quadEffect.FogColor = new Vector3(0.15f);
            //quadEffect.FogStart = 100;
            //quadEffect.FogEnd = 320;

            foreach (EffectPass pass in quadEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                graphicsDevice.DrawUserIndexedPrimitives
                    <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    _Vertices, 0, 4,
                    _Indexes, 0, 2);
            }
        }
    }
}
